﻿using System;
using System.Data;
using System.Collections.Generic;
using SERP.DAO;
namespace SERP.Controllers{
public class SalaryDataDetailsController : SalaryDataDetailsControllerBase{
	public SalaryDataDetailsController()
		: base()
	{
	}	
	private DataTable _Table;
	private SalaryDataDetailsEntity oSalaryDataDetails;	
	public SalaryDataDetailsEntity SalaryDataDetailsObject{
		get{
			return oSalaryDataDetails;
		}
		set{
			oSalaryDataDetails=value;
		}
	}
	public DataTable Table{
		get{
			if(_Table==null&&AllowCaching){				
				_Table=Cache.Get<DataTable>(CACHE_KEY_DS);
			}
			return _Table;
		}
		set{
			_Table=value;
		}
	}
	public void LoadAllDSSalaryDataDetails(bool clearCache){
		bool bForce=clearCache;
		if(bForce||_Table==null)
		{
			DataSet ds=Service.SelectAllDS();
			_Table=ds.Tables[0];
			AddIndexToTable(_Table);
			if(AllowCaching)
				AddCache(CACHE_KEY_DS,_Table);			
		}				
	}	
	public void AddIndexToTable(){
		AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		ResetTableIndex(_Table);
	}
	public void LoadObject(Int32 SalaryDataDetailId){
		SalaryDataDetailsEntity oLoad=FindSalaryDataDetails(SalaryDataDetailId);
		if(oLoad==null){
			LoadAllSalaryDataDetails(true);
			oSalaryDataDetails=FindSalaryDataDetails(SalaryDataDetailId);
		}
		else{
			oSalaryDataDetails=oLoad;
		}
	}
	public bool AddNew(){
		if(oSalaryDataDetails==null)return false;
		bool result = Service.Save(oSalaryDataDetails);
		if(result){
			AddObjectToCache(oSalaryDataDetails);
		}
		return result;
	}
	public bool Update(){
		if(oSalaryDataDetails==null)return false;
		return Service.Update(oSalaryDataDetails);	
	}
	public bool Delete(){
		if(oSalaryDataDetails==null)return false;		
		bool retvalue = Service.Delete(oSalaryDataDetails);
		if(retvalue){			
			RemoveObjectFromCache(oSalaryDataDetails);
		}
		return retvalue;
	}	
	public bool DeleteByID(Int32 SalaryDataDetailId){
		bool retvalue = Service.DeleteByID(SalaryDataDetailId);		
		if(retvalue){
			SalaryDataDetailsEntity oDeleted=FindSalaryDataDetails(SalaryDataDetailId);
			RemoveObjectFromCache(oDeleted);
		}
		return retvalue;
	}	
	public void ResetObject(){
		this.oSalaryDataDetails = new SalaryDataDetailsEntity();
	}
	public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.SalaryDataDetailsObject,row);
    }
    public SalaryDataDetailsEntity LoadSalaryDataDetails(Int32 SalaryDataDetailId){			
		SalaryDataDetailsEntity o=Service.SelectOne(SalaryDataDetailId);		
		return o;
	}
	#region Cached
    private static List<SalaryDataDetailsEntity>_ListSalaryDataDetails;
	private static bool _AllowCaching=true;
	public static bool AllowCaching{
		get{return _AllowCaching;}
		set{AllowCaching=value;}
	}	
	public static void ClearCache(){
			if(!_AllowCaching)return;
			Cache.Remove(CACHE_KEY_DS);
			Cache.Remove(CACHE_KEY_LS);
	}
	private const string CACHE_KEY_DS="SalaryDataDetailsDS";
	private const string CACHE_KEY_LS="SalaryDataDetailsLS";
	private void AddObjectToCache(SalaryDataDetailsEntity newObject){
		if(!AllowCaching)return;
		GetCacheList().Add(newObject);
	}
	private void RemoveObjectFromCache(SalaryDataDetailsEntity oRemove)
	{
		if(!AllowCaching)return;
		if(oRemove!=null)
		GetCacheList().Remove(oRemove);
	}
	private static List<SalaryDataDetailsEntity>GetCacheList(){
		if(Cache==null)return default(List<SalaryDataDetailsEntity>);
		List<SalaryDataDetailsEntity>ls= Cache.Get<List<SalaryDataDetailsEntity>>(CACHE_KEY_LS);
		if(ls==null){
			ls = new List<SalaryDataDetailsEntity>();
			AddCache(CACHE_KEY_LS,ls);
		}
		return ls;
	}
	public List<SalaryDataDetailsEntity>ListSalaryDataDetails{
		get{
			return LoadAllSalaryDataDetails(false);
		}
	}
	public static List<SalaryDataDetailsEntity>ListSalaryDataDetailsStatic{
		get{
			LoadAllSalaryDataDetails(false);
			return _ListSalaryDataDetails;
		}
	}
	public SalaryDataDetailsEntity FindSalaryDataDetails(Int32 SalaryDataDetailId){
		if(AllowCaching){
			_ListSalaryDataDetails=GetCacheList();
		}		
		SalaryDataDetailsEntity oResult=_ListSalaryDataDetails.Find(delegate(SalaryDataDetailsEntity oFind){
			bool result=true; 
			result&=oFind.SalaryDataDetailId.Equals(SalaryDataDetailId);
			return result;
		 });
		return oResult;
	}
	public static List<SalaryDataDetailsEntity>LoadAllSalaryDataDetails(bool clearCache){
		_ListSalaryDataDetails = new List<SalaryDataDetailsEntity>();
		if(AllowCaching){
			_ListSalaryDataDetails=GetCacheList();
		}
		if(_ListSalaryDataDetails==null||_ListSalaryDataDetails.Count==0)
			clearCache=true;
		if(!clearCache)return _ListSalaryDataDetails;
		_ListSalaryDataDetails=new List<SalaryDataDetailsEntity>(Service.SelectAll());		
		if(AllowCaching)
			AddCache(CACHE_KEY_LS,_ListSalaryDataDetails);
		return _ListSalaryDataDetails;
	}
	#endregion
}
}
